****APPENDIX FIGURE F4****

cd "$main_dir/output/figures/appendix_f"

graph set window fontface "Palatino"

global basic 	sex marital religion edu ethnicity birth_state
global extra	employment_status occupation income urban
global time 	survey_year age generation
global polit    party partisan

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/anes_final.dta"

//--------------------------------------------------------
//Run grid search
//--------------------------------------------------------

mat results = J(61, 3, .)
mat colnames results = lambda beta rss

local i = 1
local j = -3
local varlist wgrowth_rate_neg3 wgrowth_rate_neg2_9 wgrowth_rate_neg2_8 wgrowth_rate_neg2_7 wgrowth_rate_neg2_6 wgrowth_rate_neg2_5 wgrowth_rate_neg2_4 wgrowth_rate_neg2_3 wgrowth_rate_neg2_2 wgrowth_rate_neg2_1 wgrowth_rate_neg2 wgrowth_rate_neg1_9 wgrowth_rate_neg1_8 wgrowth_rate_neg1_7 wgrowth_rate_neg1_6 wgrowth_rate_neg1_5 wgrowth_rate_neg1_4 wgrowth_rate_neg1_3 wgrowth_rate_neg1_2 wgrowth_rate_neg1_1 wgrowth_rate_neg1 wgrowth_rate_neg0_9 wgrowth_rate_neg0_8 wgrowth_rate_neg0_7 wgrowth_rate_neg0_6 wgrowth_rate_neg0_5 wgrowth_rate_neg0_4 wgrowth_rate_neg0_3 wgrowth_rate_neg0_2 wgrowth_rate_neg0_1 wgrowth_rate_0 wgrowth_rate_0_1 wgrowth_rate_0_2 wgrowth_rate_0_3 wgrowth_rate_0_4 wgrowth_rate_0_5 wgrowth_rate_0_6 wgrowth_rate_0_7 wgrowth_rate_0_8 wgrowth_rate_0_9 wgrowth_rate_1 wgrowth_rate_1_1 wgrowth_rate_1_2 wgrowth_rate_1_3 wgrowth_rate_1_4 wgrowth_rate_1_5 wgrowth_rate_1_6 wgrowth_rate_1_7 wgrowth_rate_1_8 wgrowth_rate_1_9 wgrowth_rate_2 wgrowth_rate_2_1 wgrowth_rate_2_2 wgrowth_rate_2_3 wgrowth_rate_2_4 wgrowth_rate_2_5 wgrowth_rate_2_6 wgrowth_rate_2_7 wgrowth_rate_2_8 wgrowth_rate_2_9 wgrowth_rate_3
foreach var of local varlist {
	
	qui reghdfe trust_fed_govt `var' yob_ln_gdppc, absorb(state_code $time $basic $extra $polit) cluster(yob)
	cap drop z*
	local list trust_fed_govt `var' yob_ln_gdppc
	foreach x of local list {
		qui egen z`x' = std(`x') if e(sample)==1
	}
	
	qui reghdfe ztrust_fed_govt z`var' zyob_ln_gdppc if e(sample)==1, absorb(state_code $time $basic $extra $polit) cluster(yob)
	mat results[`i', 1] = `j'
	mat results[`i', 2] = e(b)[1,1]
	mat results[`i', 3] = e(rss)
	local ++i
	local j = `j' + 0.1
	
	di "`var' done"
}

matrix list results
svmat double results, names(col)
keep lambda beta rss
drop if lambda==.

//--------------------------------------------------------
//Create Figure F4
//--------------------------------------------------------											

scatter rss lambda, graphregion(color(white)) bgcolor(white) title("RSS", color(black)) ///
	ylabel(,labs(small) format(%9.2f)) xlabel(,labs(small)) xtitle("{&lambda}", margin(small)) ytitle("RSS", margin(small)) ///
	note("Note: each point represents a separate regression RSS per value of {&lambda} used to calculate growth experience weights. The grid search" "ranges from -3 to 3 over increments of 0.1.", size(vsmall))
	graph export "figure_f4_a.png", replace
		
															
scatter beta lambda, graphregion(color(white)) bgcolor(white) title("Coefficients", color(black)) ///
	ylabel(,labs(small) format(%9.2f)) xlabel(,labs(small)) xtitle("{&lambda}", margin(small)) ytitle("{&beta}", margin(small)) ///
	note("Note: each point represents a separate regression {&beta} per value of {&lambda} used to calculate growth experience weights. The grid search" "ranges from -3 to 3 over increments of 0.1.", size(vsmall))
	graph export "figure_f4_b.png", replace
